<template>
  <el-form :rules="rules" :model="temp" ref="dataForm">
    <!-- 提示：系统将自动会按照您的姓名，拼音，英文姓名 作为论文搜索的条件，请仔细确认。 -->
    <el-form-item label="姓名" prop="name">
      <el-input v-model="temp.name" placeholder="姓名" />
    </el-form-item>
    <el-form-item label="邮箱" prop="email">
      <el-input v-model="temp.email" placeholder="邮箱" />
    </el-form-item>
    <el-form-item label="手机" prop="phone">
      <el-input v-model="temp.phone" placeholder="手机" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="updateData">修改</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { getToken, setItem } from '@/plugins/auth'
import { isNotEmpty, zEnBase64 } from '@/plugins'

export default {
  data() {
    return {
      temp: {
        ID: '', // 用户ID
        applicationID: '', // 应用ID
        organizationID: '', // 组织机构ID
        departmentID: '', // 部门ID
        employeeID: '', // 员工ID
        name: '', // 名称
        shortName: '', // 简称
        foreignName: '', // 简称
        loginName: '', // 登录名
        loginPassword: '', // 登录密码
        confirmPassword: '', // 确认密码
        email: '', // 邮箱
        phone: '', // 手机
        avatarURL: '', // 头像url
      },
      userInfo: localStorage.getItem('userInfo') && JSON.parse(decodeURIComponent(localStorage.getItem('userInfo'))),
      rules: {
        name: [{ required: true, message: '名称不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getUserInfo()
  },
  props: {
    user: {
      type: Object,
      default: () => {
        return {
          name: '',
          email: '',
          phone: ''
        }
      }
    }
  },
  methods: {
    getUserInfo() {
      this.axios(`https://kpbase.shejidedao.cn/interface-server/api/outSystemGetOneUserDetail.json?outToken=${getToken()}&userID=${this.userInfo.userID}`).then(response=> {
        this.temp = response.body.data
        this.dialogStatus = 'update'
        this.dialogFormVisible = true
        this.disableEdit = true
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      })
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          let params = ''
          // params += 'name=' + (isNotEmpty(this.temp.name) ? zEnBase64(this.temp.name) : '')
          params += 'name=' + this.temp.name
          params += '&email=' + this.temp.email
          params += '&phone=' + this.temp.phone
          let url = `https://kpbase.shejidedao.cn/interface-server/api/outSystemUpdateOneUser.json?outToken=${getToken()}&userID=${this.userInfo.userID}&${params}&loginName=${this.userInfo.loginName}`
          this.axios(url).then(res=>{
              this.getUserInfo()
              setItem('name', this.temp.name)
              this.dialogFormVisible = false
              this.$notify({
                title: '成功',
                message: '更新成功',
                type: 'success',
                duration: 2000
              })
              this.$router.go(0);
          })
        }
      })
    },
    submit() {
      this.$message({
        message: 'User information has been updated successfully',
        type: 'success',
        duration: 5 * 1000
      })
    }
  }
}
</script>
